package com.tangbuzhi.chm.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.tangbuzhi.chm.R;
import com.tangbuzhi.chm.adapter.TabAdapter;
import com.tangbuzhi.chm.model.FragmentInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * http://git.oschina.net/tangbuzhi
 *
 * @author Tangbuzhi
 * @version V2.0
 * @Package com.tangbuzhi.chm.ui
 * @Description: ${TODO}
 * @date: 2017/9/11
 */
public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tablayout;
    private ViewPager viewpager;
    private int[] colors = new int[]{R.color.red, R.color.green, R.color.blue};
    private TabAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        viewpager = (ViewPager) findViewById(R.id.viewpager);

        adapter = new TabAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        adapter.addData(getInfos(getTitles()));
        adapter.notifyDataSetChanged();
        tablayout.setupWithViewPager(viewpager);
        viewpager.setCurrentItem(3);

        toolbar.inflateMenu(R.menu.menu_set);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.action_set) {
                    String[] strings = new String[getTitles().size()];
                    for (int i = 0; i < getTitles().size(); i++) {
                        strings[i] = getTitles().get(i);
                    }
                    Intent intent = new Intent(MainActivity.this, ChannelActivity.class);
                    intent.putExtra("channel", strings);
                    startActivityForResult(intent, 2017);
                }
                return false;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2017) {
            if (resultCode == Activity.RESULT_OK) {
                String[] chaannels = data.getStringArrayExtra("chaannel");
                List<String> strings = Arrays.asList(chaannels);
                adapter.clear();
                adapter.addData(getInfos(strings));
                adapter.notifyDataSetChanged();
                viewpager.setCurrentItem(0);
            }
        }
    }

    private List<FragmentInfo> getInfos(List<String> titles) {
        List<FragmentInfo> infos = new ArrayList<>();
        for (int i = 0; i < titles.size(); i++) {
            infos.add(new FragmentInfo(titles.get(i), TabFragment.newInstance(titles.get(i), colors[i % 3])));
        }
        return infos;
    }

    private List<String> getTitles() {
        List<String> titles = new ArrayList<>();
        titles.add("赵云");
        titles.add("哪吒");
        titles.add("蔡文姬");
        titles.add("宫本武藏");
        titles.add("李白");
        titles.add("鲁班七号");
        titles.add("阿珂");
        titles.add("狄仁杰");
        titles.add("夏侯淳");
        titles.add("孙悟空");
        titles.add("杨戬");
        titles.add("花木兰");
        titles.add("鬼谷子");
        titles.add("曹操");
        titles.add("雅典娜");
        titles.add("关羽");
        titles.add("钟馗");
        return titles;
    }
}
